package com.icoolkj.ms.pay.service.impl;

import com.icoolkj.ms.api.pay.model.RechargePackage;
import com.icoolkj.ms.api.pay.vo.RechargePackageVO;
import com.icoolkj.ms.common.core.response.ServerResponseEntity;
import com.icoolkj.ms.pay.mapper.RechargePackageMapper;
import com.icoolkj.ms.pay.service.RechargePackageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 充值套餐服务实现类
 *
 * @author icoolkj
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RechargePackageServiceImpl implements RechargePackageService {

    private final RechargePackageMapper rechargePackageMapper;

    @Override
    public ServerResponseEntity<List<RechargePackageVO>> getEnabledPackages() {
        try {
            List<RechargePackage> packages = rechargePackageMapper.selectEnabled();
            List<RechargePackageVO> result = new ArrayList<>(packages.size());
            
            for (int i = 0; i < packages.size(); i++) {
                RechargePackage pkg = packages.get(i);
                RechargePackageVO vo = new RechargePackageVO();
                BeanUtils.copyProperties(pkg, vo);
                
                // 根据套餐类型设置标签和推荐
                switch (pkg.getPackageType()) {
                    case "MONTH":
                        vo.setTagText("超值");
                        vo.setRecommended(false);
                        break;
                    case "SEASON":
                        vo.setTagText("推荐");
                        vo.setRecommended(true);
                        break;
                    case "YEAR":
                        vo.setTagText("特惠");
                        vo.setRecommended(false);
                        break;
                    default:
                        vo.setRecommended(false);
                }
                
                result.add(vo);
            }
            
            return ServerResponseEntity.success(result);
        } catch (Exception e) {
            log.error("获取充值套餐列表失败", e);
            return ServerResponseEntity.fail("获取充值套餐列表失败");
        }
    }

    @Override
    public ServerResponseEntity<RechargePackage> getPackageById(Long packageId) {
        try {
            RechargePackage rechargePackage = rechargePackageMapper.selectById(packageId);
            if (rechargePackage == null) {
                return ServerResponseEntity.fail("套餐不存在");
            }
            if (!"1".equals(rechargePackage.getStatus())) {
                return ServerResponseEntity.fail("套餐已禁用");
            }
            return ServerResponseEntity.success(rechargePackage);
        } catch (Exception e) {
            log.error("获取充值套餐详情失败", e);
            return ServerResponseEntity.fail("获取充值套餐详情失败");
        }
    }
} 